package com.td.module.equipment.controller.admin.info;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.equipment.controller.admin.info.vo.*;
import com.td.module.equipment.convert.info.InfoConvert;
import com.td.module.equipment.dal.dataobject.info.InfoDO;
import com.td.module.equipment.framework.core.RkClient;
import com.td.module.equipment.service.info.InfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 设备信息")
@RestController("equipmentInfo")
@RequestMapping("/equipment/info")
@Validated
public class InfoController {

    @Resource(name = "equipmentInfoService")
    private InfoService infoService;

    @Resource
    private RkClient rkClient;

    @PostMapping("/create")
    @ApiOperation("创建设备信息")
    public CommonResult<Long> createInfo(@Valid @RequestBody InfoCreateReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新设备信息")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody InfoUpdateReqVO updateReqVO) {
        infoService.updateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除设备信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        infoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得设备信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<InfoRespVO> getInfo(@RequestParam("id") Long id) {
        InfoDO info = infoService.getInfo(id);
        return success(InfoConvert.INSTANCE.convert(info));
    }


    @GetMapping("/sum")
    @ApiOperation("获得设备总数")
    public CommonResult<Integer> sumInfo() {
        return success(infoService.sumInfo());
    }


    @GetMapping("/list")
    @ApiOperation("获得设备信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<InfoRespVO>> getInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<InfoDO> list = infoService.getInfoList(ids);
        return success(InfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得设备信息分页")
    public CommonResult<PageResult<InfoRespVO>> getInfoPage(@Valid InfoPageReqVO pageVO) {
        PageResult<InfoDO> pageResult = infoService.getInfoPage(pageVO);
        return success(InfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出设备信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportInfoExcel(@Valid InfoExportReqVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        List<InfoDO> list = infoService.getInfoList(exportReqVO);
        // 导出 Excel
        List<InfoExcelVO> datas = InfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备信息.xls", "数据", InfoExcelVO.class, datas);
    }

    @GetMapping("/rk-list")
    @ApiOperation("获得仁科设备信息列表")
    public CommonResult<List<HashMap<String, Object>>> getRkInfoList() {
        List<HashMap<String, Object>> hashMaps = rkClient.deviceList();
        return success(hashMaps);
    }

    @GetMapping("/rk-realtime-data")
    @ApiOperation("获得仁科设备实时数据")
    public CommonResult<List<HashMap<String, Object>>> getRkRealTimeList() {
        List<HashMap<String, Object>> hashMaps = rkClient.realTimeList();
        return success(hashMaps);
    }
    @PostMapping ("/rk-history-data")
    @ApiOperation("获得仁科历史数据")
    public CommonResult<List<HashMap<String, Object>>> getRkHistoryList(@Valid @RequestBody HistoryReqVO reqVO) {
        List<HashMap<String, Object>> hashMaps = rkClient.historyList(reqVO);
        return success(hashMaps);
    }

}
